<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>table模块快速使用</title>
    <link rel="stylesheet" href="/layui/css/layui.css" media="all" />
</head>
<body>

<table id="demo" lay-filter="test"></table>
<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">批量已读</button>
        <button class="layui-btn layui-btn-sm" lay-event="delAll">批量删除</button>
    </div>
</script>
<script type="text/html" id="bar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">回复</a>
    <a class="layui-btn layui-btn-xs" lay-event="editStatus">已读</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script src="/layui/layui.js"></script>
<script src="/js/jquery-1.10.2.js"></script>
<script src="/js/my/main.js"></script>
<script>
    layui.use('table', function(){
        var table = layui.table;

        //消息列表
        var messageTable = 
        table.render({
            elem: '#demo'
            ,height: 312
            ,url: '/message/all' //数据接口
            ,toolbar: '#toolbar'
            ,title: '留言数据表'
            ,page: true //开启分页
            ,cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'id', title: 'ID', width:80, sort: true, fixed: 'left'}
                ,{field: 'name', title: '用户名', width:180}
                ,{field: 'email', title: '邮箱', width: 180, sort: true}
                ,{field: 'message', title: '留言内容', width: 280, sort: true}
                ,{fixed: 'right', title:'操作', toolbar: '#bar', width:180}
            ]]
        });
        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            //console.log(obj.event);
            switch(obj.event){
                //批量删除
                case 'delAll':
                    var data = checkStatus.data;
                    layer.confirm('确认删除这'+data.length+'行吗？', function(index){
                        
                        var iaArr = data.map(function(v){return v.id;});
                        publicCurdAjax.source = iaArr.join(',');
                        publicCurdAjax.model = 'message';
                        publicCurdAjax.DelAll();
                        
                        layer.close(index);
                        
                        messageTable.reload();
                        
                    });
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选');
                    break;
            };
        });
        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(data.id);
            if(obj.event === 'del'){
                layer.confirm('确认删除这一行吗？', function(index){
                    publicCurdAjax.source = data.id;
                    publicCurdAjax.model = 'message';
                    publicCurdAjax.Del();
                    obj.del();
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                layer.prompt({
                    formType: 2
                    ,value: data.email
                }, function(value, index){
                    obj.update({
                        email: value
                    });
                    layer.close(index);
                });
            } else if(obj.event === 'editStatus')
            {
                layer.msg("已读");
            }
            else if(obj.event === 'getCheckLength')
            {
                layer.confirm('确认删除这几行吗？', function(index){
                    // publicCurdAjax.id = data.id;
                    // publicCurdAjax.model = 'message';
                    // publicCurdAjax.D();
                    // obj.del();
                    layer.close(index);
                });
            }
        });

    });
</script>
</body>
</html>